package stack;

/**
 * Created by IntelliJ IDEA.
 * User: 杰哥
 * Date: 2020/3/27
 * Time: 10:34
 * 计算器的实现
 */
public class Calculator {

    public static void main(String[] args) {
        String expression = "80+2*6-4"; //表达式
        //创建两个栈，数栈和符合栈
        ArrayStack2 numStack = new ArrayStack2(10);
        ArrayStack2 operStack = new ArrayStack2(10);
        //定义需要的相关变量
        int index = 0; //用于扫描
        int num1 = 0; //进行计算的数
        int num2 = 0;
        int oper = 0; //运算符
        int res = 0;  // 保存一个计算的结果
        char ch = ' '; //将每次扫描得到char保存到ch
        String keepNum = "";
        //开始while循环扫描expression
        while(true) {
            //依次得到expression的每一个字符
            ch = expression.substring(index, index+1).charAt(0);
            if(isOper(ch)) {  //如果是运算符
                //判断当前符合栈是否为空
                if(!operStack.isEmpty()) { //不为空时
                    //如果符合栈有操作符就进行比较，如果当前的操作符的的优先级小于或者等于栈中的操作符。就需要从数栈中pop出两个数
                    //在从符合栈中pop出一个符合，进行运算，将得到的结果，入数栈，然后将当前的操作符入符合栈
                    if(priority(ch) <= priority(operStack.peek())) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = cal(num1,num2,oper);
                        numStack.push(res);
                        operStack.push(ch);
                    }else {
                        operStack.push(ch);
                    }
                }else { //符合栈为空直接放入
                    operStack.push(ch);
                }

            }else { //如果是数字
                //处理多位数
                keepNum += ch;
                if(index == expression.length() - 1) {
                    numStack.push(Integer.parseInt(keepNum));
                }else {
                    //判断下一个字符是不是数字，如果是数字就继续扫描，如果运算符则入栈
                    if(isOper(expression.substring(index+1,index+2).charAt(0))) {
                        numStack.push(Integer.parseInt(keepNum));
                        keepNum = "";
                    }
                }

            }
            index++;
            if(index >= expression.length()) {
                break;
            }

        }
        while (true) {
            if(operStack.isEmpty()) {
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            res = cal(num1, num2, oper);
            numStack.push(res);
        }
        System.out.println(expression+" = " + numStack.peek());

    }
    //计算方法
    public static int cal(int num1, int num2, int oper) {
        int res = 0;
        switch (oper) {
            case '+' :
                res = num1 + num2;
                break;
            case '-' :
                res = num2 - num1;
                break;
            case '*' :
                res = num1 * num2;
                break;
            case '/' :
                res = num2 / num1;
                break;
        }
        return res;
    }

    //返回运算符的优先级
    public static int priority(int oper) {
        if(oper == '*' || oper == '/') {
            return 1;
        }else if(oper == '+' || oper == '-') {
            return 0;
        }
        return -1;
    }

    //判断是不是一个运算符
    public static boolean isOper(char val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

}

//先创建一个栈
class ArrayStack2 {
    private int maxSize;
    private int[] stack;
    private int top = -1;
    //构造器
    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }

    //查看栈顶的值
    public int peek() {
        return stack[top];
    }

    //栈满
    public boolean isFull() {
        return top == maxSize - 1;
    }

    //栈空
    public boolean isEmpty() {
        return top == -1;
    }

    //入栈-push
    public void push(int value) {
        if(isFull()) {
            throw  new RuntimeException("栈满无法入栈！");
        }
        top++;
        stack[top] = value;
    }

    //出栈
    public int pop() {
        if(isEmpty()) {
            throw  new RuntimeException("栈空无法出栈！");
        }
        int value = stack[top];
        top--;
        return value;
    }

    //大小
    public int size() {
        return top + 1;
    }

    public void list() {
        if(isEmpty()) {
            System.err.println("栈空");
            return;
        }
        for(int i = top; i>=0; i--) {
            System.out.printf("stack[%d]=%d\n", i, stack[i]);
        }
    }





}


